/* eslint-disable */

export default {
    data() {
        return {
            currentDay: 1,
            currentMonth: 1,
            currentYear: 1970,
            currentWeek: 1,
            days: [],
            weeks: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日', ],
        }
    },
    created: function () { //在vue初始化时调用
        this.initData();
    },
    methods: {
        initData(dateStr) {
            this.days = [];
            let now = dateStr ? new Date(dateStr) : new Date();
            this.currentYear = now.getFullYear();
            this.currentMonth = now.getMonth() + 1;

            // 获取所在月第一天
            let date = new Date(this.formatDate(now.getFullYear(), now.getMonth() + 1, 1));

            this.currentWeek = date.getDay(); // 1...6,0 
            if (this.currentWeek == 0) {
                this.currentWeek = 7;
            }
            let str = this.formatDate(this.currentYear, this.currentMonth, 1);

            // 今天是周日，放在第一行第7个位置，前面6个
            //初始化本周
            for (let i = this.currentWeek - 1; i >= 0; i--) {
                let d = new Date(str);
                d.setDate(d.getDate() - i);
                let dayobject = {}; //用一个对象包装Date对象  以便为以后预定功能添加属性
                dayobject.day = d;
                this.days.push(dayobject); //将日期放入data 中的days数组 供页面渲染使用
            }
            //其他周
            for (let i = 1; i <= 35 - this.currentWeek; i++) {
                let d = new Date(str);
                d.setDate(d.getDate() + i);
                let dayobject = {};
                dayobject.day = d;
                this.days.push(dayobject);
            }

        },
        pickPre() {
            // setDate(0); 上月最后一天
            // setDate(-1); 上月倒数第二天
            // setDate(dx) 参数dx为 上月最后一天的前后dx天
            let d = new Date(this.formatDate(this.currentYear, this.currentMonth, 1));
            d.setDate(0);
            this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
        },
        pickNext() {
            let d = new Date(this.formatDate(this.currentYear, this.currentMonth, 1));
            d.setDate(35);
            this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
        },
        // 返回 类似 2016-01-02 格式的字符串
        formatDate(year, month, day) {
            let y = year;
            let m = month;
            if (m < 10) m = "0" + m;
            let d = day;
            if (d < 10) d = "0" + d;
            return y + "-" + m + "-" + d
        },

        isCurrentDate(date) {

            if (date.getFullYear() == new Date().getFullYear() &&
                date.getMonth() == new Date().getMonth() &&
                date.getDate() == new Date().getDate()) {
                return true
            }
            return false
        }
    },
}
